package com.jiangore.service.impl;

import com.jiangore.dao.IMemberDao;
import com.jiangore.entity.Member;
import com.jiangore.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-03-08 10:09 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */

@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private IMemberDao dao;

    @Override
    public Member selectById(Integer id) {
        Optional<Member> optional = dao.findById(id);
        return optional.get();
    }

    @Override
    public Member findByAccount(String account) {
        return dao.findByAccount(account);
    }

    @Override
    public Member findByEmail(String email) {
        return dao.findByEmail(email);
    }

    @Override
    public Member findByMobile(String mobile) {
        return dao.findByMobile(mobile);
    }

    @Override
    public List<Member> selectAll(int pageNum, int pageSize) {
        Sort sort = Sort.by(Sort.Order.desc("id"), Sort.Order.desc("account"));
        Pageable p = PageRequest.of(pageNum, pageSize, sort);
        Page<Member> ms = dao.findAll(p);
        return ms.toList();
    }

    @Override
    public int insert(Member m) {
        dao.save(m);
        return 1;
    }

    @Override
    public int update(Member m) {
        dao.save(m);
        return 1;
    }

    @Override
    public int delete(int id) {
        dao.deleteById(id);
        return 1;
    }
}
